﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;
using hbse.DTO;
using hbse.Service;

namespace hbse
{
    public partial class Uspacking : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private Users users;

        private UntilService untilService = new UntilService();
        private SampleFolderMService sampleFolderMService = new SampleFolderMService();
        private NamePlateCoreService namePlateCoreService = new NamePlateCoreService();

        private BarTender.ApplicationClass btApp;
        private BarTender.Format btformat;

        private List<DTO.ProductSamMDTO> productSamMDTOs = new List<ProductSamMDTO>();
        private DTO.ProductSamMDTO productSamMDTO = null;

        private Orders order;
        private Product mproduct;

        private NamePlatePackingService namePlatePackingService = new NamePlatePackingService();


        public Uspacking(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session, Users _users)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _users;

            try
            {
                btApp = new BarTender.ApplicationClass();
            }
            catch (Exception ex)
            {

                throw ex;
            }

            rdbroll.Checked = true;
            panelroll.Visible = true;
            panelsheet.Visible = false;
            panelfan.Visible = false;

        }

        private void Uspacking_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtSaleOrder.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtSaleOrder.Focus();
                return;
            }


            NameplatePacking newNameplatePacking = new NameplatePacking();
            newNameplatePacking.Orders = order;
            newNameplatePacking.Product = mproduct;

            newNameplatePacking.LotNo =txtlotnumber.Text.Trim();

            if (panelroll.Visible == true && panelsheet.Visible ==false && panelfan.Visible ==false) {
                newNameplatePacking.PackingRq = "";
                newNameplatePacking.RqRd = "";
                newNameplatePacking.RqPcsPerUnit = txtnumroll.Text.Trim();
                newNameplatePacking.RqUnitPerPack = txtroll.Text.Trim();
                newNameplatePacking.RqPcsOver = txtpcsroll.Text.Trim();
            }
            else if (panelroll.Visible == false && panelsheet.Visible == true && panelfan.Visible == false) {
                newNameplatePacking.PackingRq = "";
                newNameplatePacking.RqRd = "";
                newNameplatePacking.RqPcsPerUnit = txtnumsheet.Text.Trim();
                newNameplatePacking.RqUnitPerPack = txtpacksheet.Text.Trim();
                newNameplatePacking.RqPcsOver = txtcheetpcs.Text.Trim();
            }
            else if (panelroll.Visible == false && panelsheet.Visible == false && panelfan.Visible == true)
            {
                newNameplatePacking.PackingRq = "";
                newNameplatePacking.RqRd = "";
                newNameplatePacking.RqPcsPerUnit = txtnumfan.Text.Trim();
                newNameplatePacking.RqUnitPerPack = "";
                newNameplatePacking.RqPcsOver = "";
            }

           

            newNameplatePacking.InspectBy =txtinspecby.Text.Trim();
            newNameplatePacking.PrintNum =1;
            newNameplatePacking.Users = users;
            newNameplatePacking.AddDate= DateTime.Now;
            newNameplatePacking.Status = "1";


            int addresult = namePlatePackingService.addNamePlatePacking(newNameplatePacking, session);
            if (addresult > -1)
            {
                Console.WriteLine("Add Complete");
                // MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                Console.WriteLine("Add Not Complete");
                // MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



            List<NameplatePackingPrint> nameplatePackingPrints = new List<NameplatePackingPrint>();
            NameplatePackingPrint nameplatePackingPrint;

            nameplatePackingPrint = new NameplatePackingPrint();
            nameplatePackingPrint.NameplatePacking = newNameplatePacking;
            nameplatePackingPrints.Add(nameplatePackingPrint);



            // print barcode
            try
            {

                string path = Application.StartupPath + "\\NameplatePacking.btw";

                btApp.Visible = false;
                btformat = btApp.Formats.Open(path, true, "");

                // barcode 
                btformat.SetNamedSubStringValue("productcode", mproduct.ProductCode);
                btformat.SetNamedSubStringValue("productdesc", mproduct.ProductDescription);
                btformat.SetNamedSubStringValue("customercode", mproduct.Customer.CustomerCode);
                btformat.SetNamedSubStringValue("saleorder", txtSaleOrder.Text.Trim());
                btformat.SetNamedSubStringValue("lotno", txtlotnumber.Text.Trim());
                btformat.SetNamedSubStringValue("qty", newNameplatePacking.RqPcsPerUnit);
                btformat.SetNamedSubStringValue("roll", newNameplatePacking.RqUnitPerPack);
                btformat.SetNamedSubStringValue("orver", newNameplatePacking.RqPcsOver);
                string _date2 = String.Format("{0:dd/MM/yyyy}", DateTime.Now);
                 btformat.SetNamedSubStringValue("deliverydate", _date2);
                btformat.SetNamedSubStringValue("packingby", txtinspecby.Text.Trim());
                btformat.SetNamedSubStringValue("barcode", "1"+txtlotnumber.Text.Trim()+""+StringExt.StringFromLeft(txtSaleOrder.Text.Trim(),3));

                btformat.PrintOut(false, false);


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                btformat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
            }


            newNameplatePacking.NameplatePackingPrints = nameplatePackingPrints;

            int updateresult = namePlatePackingService.updateNamePlatePacking(newNameplatePacking, session);
            if (updateresult > -1)
            {
                Console.WriteLine("update Complete");
                MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                Console.WriteLine("update Not Complete");
                MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                hbse.DTO.OrderDTO orderDTO = untilService.getOrderBySaleOrder(txtSearch.Text.Trim(), session);

                order = untilService.getOrderNotDTOBySaleOrder(txtSearch.Text.Trim(),session);

                // ตรวจสอบ product มีลูกหรือไม่
                Product mproduct = sampleFolderMService.getByCode(orderDTO.ProductCode, session);

                // ถ้าไม่มีลูก
                if (mproduct.ProductType == "0")
                {
                    productSamMDTO = new ProductSamMDTO();
                    productSamMDTO.ProductCode = mproduct.ProductCode;
                    productSamMDTO.AC = mproduct.ProductAc;
                    productSamMDTO.AR = mproduct.ProductAr;
                    productSamMDTO.GAB = mproduct.ProductGab;
                    productSamMDTO.CustomerCode = mproduct.Customer.CustomerCode;
                    productSamMDTOs.Add(productSamMDTO);

                }
                else if (mproduct.ProductType == "1")
                {
                    //1. add ตัวแม่
                    productSamMDTO = new ProductSamMDTO();
                    productSamMDTO.ProductCode = mproduct.ProductCode;
                    productSamMDTO.AC = mproduct.ProductAc;
                    productSamMDTO.AR = mproduct.ProductAr;
                    productSamMDTO.GAB = mproduct.ProductGab;
                    productSamMDTO.CustomerCode = mproduct.Customer.CustomerCode;
                    productSamMDTOs.Add(productSamMDTO);

                    //2. add ตัวลูก
                    List<Product> products = untilService.getProductsByCodepType(mproduct.ProductCode, session);

                    foreach (Product p in products)
                    {
                        productSamMDTO = new ProductSamMDTO();
                        productSamMDTO.ProductCode = p.ProductCode;
                        productSamMDTO.AC = p.ProductAc;
                        productSamMDTO.AR = p.ProductAr;
                        productSamMDTO.GAB = p.ProductGab;
                        productSamMDTO.CustomerCode = p.Customer.CustomerCode;
                        productSamMDTOs.Add(productSamMDTO);
                    }

                }


                if (productSamMDTOs.Count() > 0)
                {
                    dgvProduct.DataSource = productSamMDTOs;
                    FormatdgvProduct();
                }

                txtSaleOrder.Text = orderDTO.SaleOrder;
                txtMRPNo.Text = orderDTO.MrpNo;
                txtQtyOrder.Text = orderDTO.QtyOrder;
                txtFgSgtock.Text = orderDTO.FGStock;
                txtQty.Text = orderDTO.Qty;

            }

        }

        private void FormatdgvProduct()
        {
            if (dgvProduct.RowCount > 0)
            {
                dgvProduct.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct.Columns["AC"].HeaderText = "AC";
                dgvProduct.Columns["AR"].HeaderText = "AR";
                dgvProduct.Columns["GAB"].HeaderText = "GAB";
                dgvProduct.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct.Columns["Id"].Visible = false;

            }
        }

        private void rdbroll_Click(object sender, EventArgs e)
        {
            rdbsheet.Checked = false;
            rdbfan.Checked = false;
            panelroll.Visible = true;
            panelsheet.Visible = false;
            panelfan.Visible = false;
        }

        private void rdbsheet_Click(object sender, EventArgs e)
        {
            rdbroll.Checked = false;
            rdbfan.Checked = false;
            panelroll.Visible = false;
            panelsheet.Visible = true;
            panelfan.Visible = false;
        }

        private void rdbfan_Click(object sender, EventArgs e)
        {
            rdbroll.Checked = false;
            rdbsheet.Checked = false;
            panelroll.Visible = false;
            panelsheet.Visible = false;
            panelfan.Visible = true;
        }

        private void dgvProduct_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            mproduct = sampleFolderMService.getByCode(dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString(), session);
            txtprodesc.Text = mproduct.ProductDescription;
        }
    }
}
